package com.example.secondhandshop.model;

import android.util.Log;

import com.example.secondhandshop.bean.Item;
import com.example.secondhandshop.bean.ResponseBody;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class GoodsDetailModel {
    private static final String BASE_URL = "https://api-store.openguet.cn/api/member/tran/goods/details/";
    private static final String APP_ID = "2de64479320b423cbc05dd7e6c922999";
    private static final String APP_SECRET = "16786e5aa3e8a7f8e4f24940cb351fad802ca";
    private  OkHttpClient client2 = new OkHttpClient();

    private Call currentCall; // 保存当前请求的引用

    public void getItems(long goodsId, OnProductsFetchedListener listener) {
        // 构建参数
        StringBuilder urlBuilder = new StringBuilder(BASE_URL + "?goodsId=" + goodsId);

        Log.d("GoodsDetailModel", "请求的URL: " + urlBuilder);

        // 创建请求
        Request request = new Request.Builder()
                .url(urlBuilder.toString())
                .get()
                .addHeader("appId", APP_ID)
                .addHeader("appSecret", APP_SECRET)
                .build();

        // 发送请求
        currentCall = client2.newCall(request); // 保存请求的调用
        currentCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                // 检查请求是否已被取消
                if (call.isCanceled()) {
                    listener.onFailure("请求已被取消");
                    return;
                }
                listener.onFailure(e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.e("GoodsDetailModel", "onResponse: " + response);

                if (response.isSuccessful()) {
                    String apiResponseBody = response.body().string();
                    Log.d("GoodsDetailModel", "API Response Body: " + apiResponseBody);
                    Gson gson = new Gson();

                    // 使用 TypeToken 明确指定解析的对象类型
                    ResponseBody<Item> responseBody = gson.fromJson(apiResponseBody, new TypeToken<ResponseBody<Item>>(){}.getType());

                    // 检查响应代码
                    if (responseBody.getCode() == 200) {
                        Item item = responseBody.getData();
                        listener.onSuccess(item);
                    } else {
                        listener.onFailure(responseBody.getMsg());
                    }
                } else {
                    listener.onFailure(response.message());
                }
            }
        });
    }

    // 取消当前请求
    public void cancelPendingRequests() {
        if (currentCall != null) {
            currentCall.cancel(); // 取消请求
        }
    }

    // 回调接口定义
    public interface OnProductsFetchedListener {
        void onSuccess(Item item);
        void onFailure(String error);
    }
}